package com.lmm.rpc.core.pool;

import com.lmm.rpc.core.client.NettyClient;
import com.lmm.rpc.core.cluster.RemoteServerInfo;
import io.netty.channel.Channel;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;



/**
 * @title: TPoolObjectFactory
 * @Description: 连接池化工厂
 * @Author Lmm
 * @Date: 2022/9/29 15:29
 * @Version 1.0
 */
public class TPoolObjectFactory extends BasePooledObjectFactory<Channel> {
    /**
     * 服务提供者的节点信息
     * */
    private RemoteServerInfo remoteServer;
    private int timeOut;
    private int connTimeOut;
    private boolean async;

    //通过工厂为我们生成一个客户端，单列的客户端，主要负责和server建立连接
    //主要负责生产channel
    private final NettyClient nettyClient;

    public TPoolObjectFactory(RemoteServerInfo remoteServer, int connTimeOut, int soTimeOut, boolean async,NettyClient nettyClient) {
        this.nettyClient = nettyClient;
        this.remoteServer = remoteServer;
        this.connTimeOut = connTimeOut;
        this.timeOut = soTimeOut;
        this.async = async;
    }
    //创建池里的对象
    @Override
    public Channel create() throws Exception {
        return nettyClient.getConnectChannel(remoteServer);
    }

    @Override
    public PooledObject<Channel> wrap(Channel t) {
        return new DefaultPooledObject<Channel>(t);
    }
}
